package com.example.smsalarm;

import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
	
    static Vibrator vibrator;
    static Notification notification;
    static NotificationManager notificationManager;
    private BroadcastReceiver mIntentReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);       
		vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
		
        setContentView(R.layout.activity_main);
        notificationManager=(NotificationManager)super.getSystemService(Context.NOTIFICATION_SERVICE); 
      //Listen for button clicks
        Button button = (Button)findViewById(R.id.submit);
        button.setOnClickListener(listenSms);
        
    }
    
    static boolean vibrate_on = true;
    static String KeyWord = null;
    int timer = 1;
    String alarm = null;
    long[] vibratepattern = {100, 400, 500, 400};
    @Override
    protected void onResume() {
    super.onResume();
    IntentFilter intentFilter = new IntentFilter("SmsMessage.intent.MAIN");
    mIntentReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
    String msg = intent.getStringExtra("get_msg");
    
    	//Process the sms format and extract body &amp; phoneNumber
    	msg = msg.replace("\n", "");
        if(KeyWord != null) {
    	if(msg.contains(KeyWord)) {
    		if(vibrate_on) {	  
    			Uri alarmSound = Uri.parse("android.resource://com.example.smsalarm/" + R.raw.alarm);
				notification = new Notification.Builder(context)
		         .setContentTitle("You Have a SMS Alarm "+KeyWord)
		         .setContentText("")
		         .setSmallIcon(R.drawable.ic_launcher)
		         .setSound(alarmSound)
		         .setVibrate(vibratepattern)
		         .build();
				
				MainActivity.notification.flags |= Notification.FLAG_AUTO_CANCEL;

				MainActivity.notificationManager.notify(0, MainActivity.notification);
				}
    	}
        }
    	}
    	};
    	this.registerReceiver(mIntentReceiver, intentFilter);
    	}
    
    private OnClickListener listenSms = new OnClickListener() {
    	public void onClick(View v) {
    		// Intent intent = new Intent(MainActivity.this, TextMessageReceiver.class);
    		 EditText fieldfilter = (EditText)findViewById(R.id.filter);
    		 if(fieldfilter.getText().toString().length() != 0) {
    		    KeyWord = fieldfilter.getText().toString();
    		 }
    		 EditText fieldalarm = (EditText)findViewById(R.id.alarm);
    		 alarm = fieldalarm.getText().toString();
    		 
    		 EditText fieldtimer = (EditText)findViewById(R.id.timer);
    		 timer =  Integer.valueOf(fieldtimer.getText().toString());
    		 
    		 EditText fieldvibrate = (EditText)findViewById(R.id.vibrate);
    		 String vibrate = fieldvibrate.getText().toString();
    		 
    		 if(vibrate.equals("YES") || vibrate.equals("yes")) {
    			 vibrate_on = true;
    		 } else {
    			 vibrate_on = false;
    		 }
    		// startService(intent);
    	}
    };
    
    @Override
    public void onBackPressed() {
        Intent backtoHome = new Intent(Intent.ACTION_MAIN);
        backtoHome.addCategory(Intent.CATEGORY_HOME);
        backtoHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(backtoHome);
    }
}
